package facilitators;


import containers.Assembly;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import objects.Bar;
import objects.FixedMass;
import objects.Mass;
import objects.Muscle;
import objects.Spring;


/**
 * 
 * @author Robert C. Duvall edited by rwb10 & sab60
 */
public class Factory {
    /**
     * 
     * @param assembly Physics Assembly
     * @param modelFile File containing the objects to assembly.
     */
    public void loadModel (Assembly assembly, File modelFile) {
        try {
            Scanner input = new Scanner(modelFile);
            while (input.hasNext()) {
                Scanner line = new Scanner(input.nextLine());
                if (line.hasNext()) {
                    String type = line.next();
                    if ("mass".equals(type)) {
                        assembly.add(massCommand(line));
                    }
                    else if ("spring".equals(type)) {
                        assembly.add(springCommand(line, assembly));
                    }
                    else if ("muscle".equals(type)) {
                        assembly.add(muscleCommand(line, assembly));
                    }
                }
            }
            input.close();
        }
        catch (FileNotFoundException e) {
            // should not happen because File came from user selection
            e.printStackTrace();
        }
    }

    private Spring muscleCommand (Scanner line, Assembly assembly) {
        int m1 = line.nextInt();
        int m2 = line.nextInt();
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        double amplitude = line.nextDouble();
        return new Muscle(assembly.getMass(m1), assembly.getMass(m2),
                restLength, ks, amplitude);
    }

    private Mass massCommand (Scanner line) {
        int id = line.nextInt();
        double x = line.nextDouble();
        double y = line.nextDouble();
        double mass = line.nextDouble();
        if (mass < 0) {
            return new FixedMass(id, x, y, mass);
        }
        return new Mass(id, x, y, mass);
    }

    private Spring springCommand (Scanner line, Assembly assembly) {
        int m1 = line.nextInt();
        int m2 = line.nextInt();
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        if (ks < 0) {
            return new Bar(assembly.getMass(m1), assembly.getMass(m2));
        }
        return new Spring(assembly.getMass(m1), assembly.getMass(m2),
                restLength, ks);
    }

}
